// pages/main-music/index.js
import {getMusicBanner, getSongMenuList} from '../../services/modules/music'
import recommendStore from "../../store/recommendStore"
import rankingStore from "../../store/rankingStore"
import playerStore from "../../store/playerStore"
import querySelect from "../../utils/query-select"
// import { throttle } from 'underscore'
import thr from '../../utils/throttle'
const throttleQs = thr(querySelect, 200)

Page({
  data: {
    searchVal:'', // 搜索内容
    bannerList: [], // 轮播图
    bannerHeight: 0, // 轮播图动态高度
    recommendSongs:[], // 推荐歌曲
    hotMenuList: [], //热门歌单
    recMenuList: [], // 推荐歌单
    rankingInfos:{ // 巅峰榜数据
      newRanking: [],
      originRanking: [],
      upRanking: []
    },
  },

  onLoad(options) {
    this.getBanner()
    this.fetchSongMenuList()
    recommendStore.onState("recommendSongInfo", this.handleRecommendSongs)
    recommendStore.dispatch("fetchRecommendSongsAction")
    rankingStore.onState("newRanking", this.handleNewRanking)
    rankingStore.onState("originRanking", this.handleOriginRanking)
    rankingStore.onState("upRanking", this.handleUpRanking)
    rankingStore.dispatch("fetchRankingDataAction")
  },
  onUnload() {
    recommendStore.offState('recommendSongInfo',this.handleRecommendSongs)
    rankingStore.offState('recommendSongInfo',this.handleNewRanking)
    rankingStore.offState('recommendSongInfo',this.handleOriginRanking)
    rankingStore.offState('recommendSongInfo',this.handleUpRanking)
  },

  // 图片加载完毕
  async onBannerImageLoad(event) {
    const res = await throttleQs(".banner-image")
    this.setData({ bannerHeight: res[0].height })
  },
  // 获取轮播图
  getBanner() {
    getMusicBanner().then(res => {
      this.setData({
        bannerList: res.banners
      })
    })
  },
  // 搜索点击
  searchHandle() {
    wx.navigateTo({
      url: '/pages/detail-search/index',
    })
  },
  // 推荐歌曲点击
  onSongItemTap(item) {
    // 歌曲列表换了
    playerStore.dispatch('setPlayListSongs', this.data.recommendSongs)
  },
  // state获取流行歌曲信息
  handleRecommendSongs(value) {
    if (!value.tracks) return
    this.setData({ recommendSongs: value.tracks.slice(0, 6) })
  },
  // state获取新歌榜信息
  handleNewRanking(value) {
    if (!value.name) return
    this.setData({ isRankingData: true })
    const newRankingInfos = { ...this.data.rankingInfos, newRanking: value }
    this.setData({ rankingInfos: newRankingInfos })
  },
  // state获取原创榜信息
  handleOriginRanking(value) {
    if (!value.name) return
    this.setData({ isRankingData: true })
    const newRankingInfos = { ...this.data.rankingInfos, originRanking: value }
    this.setData({ rankingInfos: newRankingInfos })
  },
  // state获取飙升榜信息
  handleUpRanking(value) {
    if (!value.name) return
    this.setData({ isRankingData: true })
    const newRankingInfos = { ...this.data.rankingInfos, upRanking: value }
    this.setData({ rankingInfos: newRankingInfos })
  },
  // 获取热门歌单-推荐歌单
  async fetchSongMenuList() {
    getSongMenuList().then(res => {
      this.setData({ hotMenuList: res.playlists })
    })
    getSongMenuList("华语").then(res => {
      this.setData({ recMenuList: res.playlists })
    })
  },
})